package com.example.three.Controller.OfficeOrderController;


import com.example.three.DO.SysOfficeOrder;
import com.example.three.DO.SysOfficeProd;
import com.example.three.DO.SysOfficeProdComm;
import com.example.three.DO.TableData;
import com.example.three.Mapper.ShopGoodComm.GoodCommMapper;
import com.example.three.Service.OfficeOrderService.OrderService;
import com.example.three.Service.SysShopService.SysShopService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller

@RequestMapping("")
public class OrderController {
    @Resource
    private OrderService orderService;

    @Resource
    private SysShopService SysShopService;


    //查询发货订单
    @RequestMapping("To-orderlist")
    public String To_Orderlist(){

        return "/orderlist";
    }

    @RequestMapping("/orderlist")
    @ResponseBody
    public TableData orderlist(HttpSession session, Model model,@RequestParam Map<String, Object> params){
        List<SysOfficeOrder>  orderlist = (List<SysOfficeOrder>)orderService.findBy();
        String prod_name=null;
        if(params.containsKey("keyWord")){
            prod_name=params.get("keyWord").toString();
        }


        if (prod_name == null || prod_name == "") {

        }
        else {
            orderlist = (List<SysOfficeOrder>)orderService.findByselectBy(prod_name);
        }
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setData(orderlist);
        return tableData;
    }
    //发货
    @RequestMapping("/updateorder")
    public void updateorder(HttpSession session,String orderId){
        orderService.updataorder(orderId);
        SysOfficeOrder orderlist = orderService.findBy1(orderId);
        long pro_id = orderlist.getProdId();
        long number = orderlist.getTotal();
        SysOfficeProd sysOfficeProds = SysShopService.profindBy1(pro_id);
        long number1 = Long.parseLong(sysOfficeProds.getTotalStocks());
        String TotalStocks = String.valueOf((number1-number));
        SysShopService.updatenumber(pro_id,TotalStocks);
    }

    //退货订单
    @RequestMapping("To-orderlistship")
    public String To_Orderlistship(){

        return "/orderlistship";
    }

    @RequestMapping("/orderlistship")
    @ResponseBody
    public TableData orderlistship(HttpSession session, Model model,@RequestParam Map<String, Object> params){
        List<SysOfficeOrder>  orderlist = (List<SysOfficeOrder>)orderService.findByship();
        String prod_name=null;
        if(params.containsKey("keyWord")){
            prod_name=params.get("keyWord").toString();
        }


        if (prod_name == null || prod_name == "") {

        }
        else {
            orderlist = (List<SysOfficeOrder>)orderService.findByselectship(prod_name);
        }
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setData(orderlist);
        return tableData;
    }
    //退货
    @RequestMapping("/updateordership")
    public void updateordership(String orderId,HttpSession session, Model model){
        orderService.updataordership(orderId);
        List<SysOfficeOrder>  orderlist = (List<SysOfficeOrder>)orderService.findByship();
        model.addAttribute("orderlistship",orderlist);
    }

    //查询所有订单
    @RequestMapping("To-orderlistall")
    public String ToRecom(){
        return "/orderlistall";
    }

    @RequestMapping("/orderlistall")
    @ResponseBody
    public TableData orderlistall(HttpSession session, Model model,@RequestParam Map<String, Object> params){
        List<SysOfficeOrder>  orderlist = (List<SysOfficeOrder>)orderService.findByAll();
        String prod_name=null;
        if(params.containsKey("keyWord")){
            prod_name=params.get("keyWord").toString();
        }


        if (prod_name == null || prod_name == "") {

        }
        else {
            orderlist = (List<SysOfficeOrder>)orderService.findByselectall(prod_name);
        }
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setData(orderlist);
        return tableData;
    }
}
